/**
 * 创建一个模式化的dialog
 * @author fusf
 * @requires jQuery,EasyUI
 *
 */
sy.modalDialog = function (options) {
    var opts = $.extend({
        title: '&nbsp;',
        width: 550,
        height: 400,
        modal: true,
        onClose: function () {
            $(this).dialog('destroy');
        }
    }, options);
    opts.modal = true;// 强制此dialog为模式化，无视传递过来的modal参数
    if (options.url) {
        //opts.href = options.url;
        opts.content = '<iframe  src="' + options.url + '" allowTransparency="true" scrolling="auto" width="100%" height="100%" frameBorder="0" ></iframe>';

    }
    return $('<div/>').dialog(opts).dialog('center');
};


/**
 * dialog
 * @param url 请求路径
 * @param title 标题
 * @param grid 列表
 * @param width 长度
 * @param height 高度
 * @param $menuTree 菜单列表
 */
var sy_dialogFun = function (url, title, height, width, $grid, $menuTree) {
    if (!$grid) {
        $grid = $("#grid");
    }
    var dialog = parent.sy.modalDialog({
        title: title,
        url: url,
        width: width,
        height: height,
        buttons: [{
            text: '保存',
            iconCls: "icon-ok",
            handler: function () {
                dialog.find('iframe').get(0).contentWindow.sy_submitForm(dialog, $grid, parent.$, $menuTree);
            }
        }, {
            text: '取消',
            iconCls: "icon-cancel",
            handler: function () {
                dialog.dialog('destroy');
            }
        }]
    });

};
/**
 * 新增
 * @param url 请求路径
 * @param title 标题
 * @param grid 列表
 * @param width 长度
 * @param height 高度
 * @param $menuTree 菜单列表
 */
var sy_addFun = function (url, title, height, width, $grid, $menuTree) {
    url = url + "/" + 0;
    sy_dialogFun(url, title, height, width, $grid, $menuTree);
}

var sy_addFunByParentId = function (url, title, height, width, $grid, $menuTree) {
    var parentId = "0";
    if (!$grid) {
        $grid = $("#grid");
    }
    var row = $grid.treegrid("getSelected");
    if (row) {
        parentId = row.id;
    }
    url += parentId;
    sy_addFun(url, title, height, width, $grid, $menuTree);
}


/**
 * 编辑操作，url，title：，grid：，width：长度 height：高度
 * @param url 请求路径
 * @param title 标题
 * @param grid 列表
 * @param width 长度
 * @param height 高度
 * @param $menuTree 菜单列表
 */
var sy_editFun = function (url, title, height, width, $grid, $menuTree) {
    if (!$grid) {
        $grid = $("#grid");
    }
    var rows = $grid.datagrid('getSelections');
    if (rows.length == 0) {
        parent.$.messager.alert('提示', "请选择一条数据", 'error');
    } else if (rows.length > 1) {
        parent.$.messager.alert('提示', "只能选择一条数据", 'error');
    } else {
        url = url + "/" + rows[0].id;
        sy_dialogFun(url, title, height, width, $grid, $menuTree);
    }
}
/**
 * 查看操作
 * @param url 请求路径
 * @param title 标题
 * @param grid 列表
 * @param width 长度
 * @param height 高度
 * @param $menuTree 菜单列表
 */
var sy_showFun = function (url, title, height, width, $grid, $menuTree) {
    if (!$grid) {
        $grid = $("#grid");
    }
    var rows = $grid.datagrid('getSelections');
    if (rows.length == 0) {
        parent.$.messager.alert('提示', "请选择一条数据", 'error');
    } else if (rows.length > 1) {
        parent.$.messager.alert('提示', "只能选择一条数据", 'error');
    } else {
        url = url + "/" + rows[0].id;
        var dialog = parent.sy.modalDialog({
            title: title,
            width: width,
            height: height,
            url: url,
            buttons: [{
                text: '取消',
                iconCls: "icon-cancel",
                handler: function () {
                    dialog.dialog('destroy');
                }
            }]
        });

    }
}

/**
 * 支持批量删除
 * @param url
 * @param $grid
 * @param tip
 * @param $menuTree
 */
var sy_removeGridFun_tip = function (url, $grid, tip, $menuTree) {
    if (!$grid) {
        $grid = $("#grid");
    }
    if (!tip) {
        tip = "您确定要删除此记录？";
    }
    var rows = $grid.datagrid('getSelections');
    if (rows.length > 0) {
        var id;
        for (var i = 0; i < rows.length; i++) {
            if (id) {
                id += "," + rows[i].id;
            } else {
                id = rows[i].id;

            }
        }
        parent.$.messager.confirm('询问', tip, function (r) {
            if (r) {
                $.post(url, {
                    id: id
                }, function (result) {
                    if (result.success) {
                        parent.$.messager.show({
                            title: '提示',
                            msg: result.msg
                        });
                        try {
                            $grid.datagrid('load');
                            $grid.datagrid('clearSelections');
                            $grid.datagrid('clearChecked');
                            $grid.treegrid('load');
                        } catch (e) {
                        }
                        try {
                            if ($menuTree) {
                                $menuTree.tree("reload");//加载树
                            }
                        } catch (e) {

                        }
                    } else {
                        parent.$.messager.alert('提示', result.msg, 'error');
                    }

                }, 'json');
            }
        });
    } else {
        parent.$.messager.alert('提示', "请选择一条数据", 'error');
    }
};

/**
 * 分页查询
 * @param grid
 */
var sy_searchFun = function (grid, form) {
    if (!grid) {
        grid = $("#grid");
    }
    if (!form) {
        form = $("#searchForm");
    }
    grid.datagrid('load', sy.serializeObject(form));
    grid.datagrid('clearSelections');
    grid.datagrid('clearChecked');
}
/**
 * 分页查询重置
 * @param grid
 */
var sy_resetFun = function ($grid, $form) {
    if (!$grid) {
        $grid = $("#grid");
    }
    if (!$form) {
        $form = $("#searchForm");
    }
    $form.form('clear');
    $grid.datagrid('load', {});
    $grid.datagrid('clearSelections');
    $grid.datagrid('clearChecked');
}

var sy_submitForm = function ($dialog, $grid, $pjq, $menuTree) {
    debugger;
    //var $form = $dialog.find("form");
    var $form = $("form");
    var url = $form.attr("action");
    if (typeof(url) == "undefined" || url == null) {
        $pjq.messager.alert('提示', "请求地址未配置", 'error');
        return false;
    }
    /*  $dialog.find("input.no-space").each(function (index, element) {//去除左右两边的空格
     try {
     var id = $(this).attr("id");
     $dialog.find("#" + id).textbox("setValue", $.trim($dialog.find("#" + id).val()));
     } catch (e) {

     }
     });*/
    /*$("input,textarea").each(function(index, element) {//去除左右两边的空格
     $(this).val($.trim($(this).val()));
     });*/


    //自定义方法有需要就执行
    try {
        sy_customerFun();
    } catch (e) {

    }
    if ($form.form('validate')) {//校验
        parent.$.messager.progress({
            text: '数据保存中....'
        });
        $.post(url, sy.serializeObject($form), function (result) {
            parent.$.messager.progress('close');
            if (result.success) {
                $pjq.messager.show({
                    title: '提示',
                    msg: result.msg
                });
                //刷新datagrid
                try {
                    if ($grid) {
                        $grid.datagrid('load');
                        $grid.datagrid('clearSelections');
                        $grid.datagrid('clearChecked');
                        $grid.treegrid('load');
                    }
                } catch (e) {

                }

                try {
                    if ($menuTree) {
                        $menuTree.tree("reload");//加载树
                    }
                } catch (e) {

                }
                if ($dialog) {
                    $dialog.dialog('destroy');
                }
            } else {
                $pjq.messager.alert('提示', result.msg, 'error');
            }
        }, 'json');
    }
}


//全选
var checkAllTree = function (id) {
    var $tree = $("#" + id);
    var nodes = $tree.tree("getRoots");
    for (var i = 0; i < nodes.length; i++) {
        var nodes_2 = $tree.tree("getChildren", nodes[i].target);
        for (var j = 0; j < nodes_2.length; j++) {
            $tree.tree("check", nodes_2[j].target);
        }
        $tree.tree("check", nodes[i].target);
    }
}
//取消
var unCheckAllTree = function (id) {
    var $tree = $("#" + id);
    var nodes = $tree.tree("getRoots");
    for (var i = 0; i < nodes.length; i++) {
        var nodes_2 = $tree.tree("getChildren", nodes[i].target);
        for (var j = 0; j < nodes_2.length; j++) {
            $tree.tree("uncheck", nodes_2[j].target);
        }
        $tree.tree("uncheck", nodes[i].target);
    }
}

/**
 * 上传图片方法
 * @param url
 * @param title
 * @param fileElemenId
 * @param success 成功后处理方法  写法：function(result){fn();}
 * @param type
 * @param dataType
 */
var sy_uploadImg = function(url,title,fileElemenId,type,dataType){
    debugger;
    var options = {url:url,title:title,fileElemenId:fileElemenId,
                type:type,dataType:dataType};
        var opts = $.extend({
            dataType: 'text',
            type: "post",
            url: "/sys/upload/list",
            fileElementId: 'fileToUpload',//file标签的id
            success: function(result){
                successFun(result);
            }
        }, options);
        $.ajaxFileUpload(opts);
}

var successFun = function(result) {
    return;
}

/**
 * 删除id为id的元素
 * @param id
 */
var delId = function(id) {
    $("#"+id).remove();
}


$(function(){
    $(".dialog_form #fm .dialog_item").each(function (index, element) {
        var re_obj = $(this).find("input").attr("required");
        if(re_obj=="required") {
            $(this).append("<span class='required'>*</span>");
        }
    });
});